package cn.school.stu.evaluation.service.impl;

import cn.school.common.utils.SysUserUtils;
import cn.school.stu.evaluation.dao.StuEvaIndicatorDao;
import cn.school.stu.evaluation.entity.StuEvaIndicator;
import cn.school.stu.evaluation.service.StuEvaIndicatorService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author: lx
 * @date: 2023/5/24 11:07
 * @description:
 */
@Service
public class StuEvaIndicatorServiceImpl extends ServiceImpl<StuEvaIndicatorDao,StuEvaIndicator> implements StuEvaIndicatorService {

    @Autowired
    private StuEvaIndicatorDao stuEvaIndicatorDao;

    @Override
    public int createStuEvaIndicator(StuEvaIndicator stuEvaIndicator) {
        if(StringUtils.isEmpty(stuEvaIndicator.getId())){
            if(StringUtils.isEmpty(stuEvaIndicator.getPid())){
                stuEvaIndicator.setPid(0L);
            }
            return stuEvaIndicatorDao.insert(stuEvaIndicator);
        }else{
            stuEvaIndicator.setModifyTime(new Date());
            return stuEvaIndicatorDao.updateById(stuEvaIndicator);
        }

    }


    @Override
    public List<StuEvaIndicator> findFirstLevelIndicator() {
        LambdaQueryWrapper<StuEvaIndicator> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StuEvaIndicator::getType,"0").eq(StuEvaIndicator::getTenantId,SysUserUtils.getSysUser().getTenantId());
        return stuEvaIndicatorDao.selectList(wrapper);
    }

    @Override
    public List<StuEvaIndicator> findStuEvaIndicatorPageList(Map<String,Object> query) {
        String tenantId = SysUserUtils.getSysUser().getTenantId();

        List<StuEvaIndicator> list = stuEvaIndicatorDao.findStuEvaIndicatorPageList(tenantId,"0");
        for (StuEvaIndicator stuEvaIndicator : list) {
            stuEvaIndicator.setChildren(stuEvaIndicatorDao.findStuEvaIndicatorPageList(tenantId,stuEvaIndicator.getId()+""));
        }

        return list;
    }

    @Override
    public List<StuEvaIndicator> findStuEvaIndicatorList(String id) {
        List<StuEvaIndicator> indicator = new ArrayList<>();
        String[] split = id.split(",");
        for (int j=0;j<split.length;j++){
            String sid = split[j];
            indicator.add(stuEvaIndicatorDao.findStuEvaIndicatorById(sid));
        }

        String tenantId = SysUserUtils.getSysUser().getTenantId();

        List<StuEvaIndicator> list = new ArrayList<>();
        for (int i = 0;i<indicator.size();i++) {
            list.add(stuEvaIndicatorDao.findStuEvaIndicatorList(tenantId, "0", indicator.get(i).getId()));
            for (int k = 0; k < list.size(); k++) {
                list.get(k).setChildren(stuEvaIndicatorDao.findStuEvaIndicatorPageList(tenantId, list.get(k).getId() + ""));
            }
        }
        return list;
    }

    @Override
    public int deleteStuEvaIndicatorById(Long id) {
        return this.baseMapper.deleteById(id);
    }
    
    @Override
    public  Map<String, List<StuEvaIndicator>> getChildrenIndicatorById(String id) {
        String tenantId = SysUserUtils.getSysUser().getTenantId();
        String[] split = id.split(",");
        Map<String, List<StuEvaIndicator>> resultMap = new HashMap<>();
        for (String s : split) {
            List<StuEvaIndicator> stuEvaIndicatorPageList = stuEvaIndicatorDao.findStuEvaIndicatorPageList(tenantId, s);
            resultMap.put(s, stuEvaIndicatorPageList);
        }
        return resultMap;
    }
}
